In [1]:
%matplotlib inline
import cPickle
import pickle
import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from termcolor import colored
In [2]:
def plot_confusion_matrix(y_test,y_pred, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
import pandas as ps
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
pd.DataFrame(cm).to_csv("confusion_2.csv", sep=",",header = list(classes))
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
In [3]:
import numpy as np
import re
from scipy import linalg
import scipy.ndimage as ndi
from six.moves import range
import os
import threading
def transform_matrix_offset_center(matrix, x, y):
o_x = float(x) / 2 + 0.5
o_y = float(y) / 2 + 0.5
offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
return transform_matrix
def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', cval=0.):
x = np.rollaxis(x, channel_index, 0)
final_affine_matrix = transform_matrix[:2, :2]
final_offset = transform_matrix[:2, 2]
channel_images = [ndi.interpolation.affine_transform(x_channel, final_affine_matrix,
final_offset, order=0, mode=fill_mode, cval=cval) for x_channel in x]
x = np.stack(channel_images, axis=0)
x = np.rollaxis(x, 0, channel_index+1)
return x
def image_rotation(x, rg,step, row_index=0, col_index=1, channel_index=2,
fill_mode='nearest', cval=0.):
image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
for i in range(-rg,rg,step):
theta = (np.pi * i)/ 180
rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
[np.sin(theta), np.cos(theta), 0],
[0, 0, 1]])
h, w = x.shape[row_index], x.shape[col_index]
transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)
new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
return image_set
def image_shear(x, row_index=0, col_index=1, channel_index=2,
fill_mode='nearest', cval=0.):
shear = [-0.25,0.25,6.3,9.5,-9.5,2.95,-2.95,0]
image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
for i in shear:
shear_matrix = np.array([[1, -np.sin(i), 0],
[0, np.cos(i), 0],
[0, 0, 1]])
h, w = x.shape[row_index], x.shape[col_index]
transform_matrix = transform_matrix_offset_center(shear_matrix, h, w)
new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
return image_set[1:]
def image_zoom(x, zoom_range,step, row_index=0, col_index=1, channel_index=2,
fill_mode='nearest', cval=0.):
if len(zoom_range) != 2:
raise Exception('zoom_range should be a tuple or list of two floats. '
'Received arg: ', zoom_range)
if zoom_range[0] == 1 and zoom_range[1] == 1:
zx, zy = 1, 1
else:
image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
for zx in np.arange(zoom_range[0],zoom_range[1],step):
for zy in np.arange(zoom_range[0],zoom_range[1],step):
zoom_matrix = np.array([[zx, 0, 0],
[0, zy, 0],
[0, 0, 1]])
h, w = x.shape[row_index], x.shape[col_index]
transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
return image_set[1:]
def image_shift(x, wrg, hrg,step ,row_index=0, col_index=1, channel_index=2,
fill_mode='nearest', cval=0.):
h,w = x.shape[row_index],x.shape[col_index]
image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
for tx in np.arange(-hrg*h,hrg*h,step):
for ty in np.arange(-wrg*h,wrg*h,step):
translation_matrix = np.array([[1, 0, tx],
[0, 1, ty],
[0, 0, 1]])
transform_matrix = translation_matrix # no need to do offset
new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
return image_set[1:]
In [14]:
def load_data():
import pandas as pd
import numpy as np
from PIL import Image
from cv2 import resize
#from skimage.transform import resize
from skimage import exposure
train = pd.read_csv('/home/mckc/Downloads/bigdb////train.csv')
test = pd.read_csv('/home/mckc/Downloads/bigdb////test.csv')
#train = train.query('subject in ("Dhruva Murari","Ponraj S","Naresh Raj","Raashi Chhalani","naveen","Praba")')
#test = test.query('subject in ("Dhruva Murari","Ponraj S","Naresh Raj","Raashi Chhalani","naveen","Praba")')
print 'the training data shape is ',train.shape
print 'the test data shape is ', test.shape
X_tr = []
Y_tr = []
iteration = 0
for i in train.values[:,0]:
#image = exposure.equalize_hist(resize(np.array(Image.open(i).convert('L')),(96,96)))
image=resize(np.array(Image.open(i)),(224,224))
image = cv2.cvtColor(image,cv2.COLOR_GRAY2BGR)
#print image.shape
X_tr.append(image)
Y_tr.append(train.values[iteration,1])
iteration+=1
if iteration % 5000==0:
print colored((float(iteration)/len(train.values[:,0])*100 ,' Percentage complete'), 'green')
X_ts = []
Y_ts = []
iteration = 0
for i in test.values[:,0]:
#image = exposure.equalize_hist(resize(np.array(Image.open(i).convert('L')),(96,96)))
image=resize(np.array(Image.open(i)),(224,224))
image = cv2.cvtColor(image,cv2.COLOR_GRAY2BGR)
X_ts.append(image)
Y_ts.append(test.values[iteration,1])
iteration+=1
if iteration % 5000==0:
print colored((float(iteration)/len(test.values[:,0])*100 ,' Percentage complete'), 'green')
X_tr,X_ts,Y_tr,Y_ts = np.array(X_tr),np.array(X_ts),np.array(Y_tr),np.array(Y_ts)
print 'the training file shape',X_tr.shape,Y_tr.shape
print 'the testing file shape',X_ts.shape,Y_ts.shape
return X_tr,X_ts,Y_tr,Y_ts
In [15]:
def simulate(X,Y):
import scipy as sp
import scipy.ndimage
complete = np.zeros((1,96,96),dtype=np.uint8)
Y_complete = []
for i in range(len(X)):
#complete = np.vstack((complete,X[i,:,:].reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 15,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -15,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -10,reshape=False,cval=255).reshape(1,96,96)))
rotated = np.fliplr(X[i,:,:])
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 15,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -15,reshape=False,cval=255).reshape(1,96,96)))
#complete = np.vstack((complete,rotated.reshape(1,96,96)))
Y_complete = np.append(Y_complete,([Y[i]]*12))
if i % 50==0:
print colored((float(i)/len(X)*100 ,' Percentage complete'),'green')
complete = complete[1:,:,:]
return complete,Y_complete
In [16]:
def augment(X,Y):
import skimage.transform as tf
import scipy as sp
import scipy.ndimage
complete = np.zeros((1,96,96),dtype=np.uint8)
Y_complete = []
for i in range(len(X)):
#complete = np.vstack((complete,X[i,:,:].reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(0,-5))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(0,5))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(5,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(-5,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(0,-5))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(0,5))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(5,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(-5,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(0,-5))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(0,5))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(5,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(-5,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(0,-3))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(0,3))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(3,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1,translation=(-3,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(0,-3))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(0,3))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(3,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=1.2,translation=(-3,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(0,-3))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(0,3))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(3,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.SimilarityTransform(scale=0.8,translation=(-3,0))
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=0.25)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=0.2)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=0.15)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=0.1)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=-0.25)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=-0.2)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=-0.15)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
tform = tf.AffineTransform(scale=(1,1),shear=-0.1)
complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
rotated = np.fliplr(X[i,:,:])
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 15,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -15,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 15,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -5,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -10,reshape=False,cval=255).reshape(1,96,96)))
complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -15,reshape=False,cval=255).reshape(1,96,96)))
Y_complete = np.append(Y_complete,([Y[i]]*44))
if i % 50==0:
print colored((float(i)/len(X)*100 ,' Percentage complete'),'green')
complete = complete[1:,:,:]
return complete,Y_complete
In [17]:
def Save_data(X,Y,data):
for i in range(len(X)):
file_name = '/home/mckc/face_'+data+'/'+Y[i]+'_'+str(i)+'.npy'
np.save(file_name,X[i,:,:])
In [18]:
def load(data):
import os
import numpy as np
files = os.listdir('/home/mckc/face_'+data+'/')
X = np.zeros((1,96,96),dtype=np.float64)
Y = []
iter = 0
for i in files:
X = np.vstack((X,np.load('/home/mckc/face_'+data+'/'+i).reshape(1,96,96)))
index = i.index('_')
Y = np.append(Y,i[:index])
iter = iter+1
if iter % 800 ==0:
print colored((float(iter)/len(files)*100 ,' Percentage complete'), 'green')
print X[1:,:,:].shape,Y.shape
return X[1:,:,:],Y
In [19]:
X_train,X_ts,Y_train,Y_ts = load_data()
#X_tr,X_ts,Y_tr,Y_ts = load_data()
In [20]:
%%time
#X_tr,Y_tr = simulate(X_train,Y_train)
#X_tr,Y_tr = augment(X_train,Y_train)
X_tr,Y_tr = (X_train,Y_train)
print X_tr.shape,Y_tr.shape
In [21]:
from sklearn.utils import shuffle
X_tr,Y_tr = shuffle(X_tr,Y_tr)
#X_normal = X_tr.reshape(-1,9216)
#X_test_normal = X_ts.reshape(-1,9216)
map, Y_number = np.unique(Y_tr, return_inverse=True)
Y_test_number = np.unique(Y_ts, return_inverse=True)[1]
Y_number = Y_number.astype("|S6")
len(map)
Y_test_number = Y_test_number.astype("|S6")
np.save('/home/mckc/map_big',map)
In [22]:
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle
In [24]:
#VGG is BGR
from keras.models import Sequential
from keras.utils import np_utils
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
Y_Keras = np_utils.to_categorical(Y_number, 2691)
Y_Keras_test = np_utils.to_categorical(Y_test_number,2691)
def VGG_16(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
model = VGG_16('/home/mckc/Downloads/vgg16_weights.h5')
model = VGG_16()
model.layers.pop()
model.add(Dense(2691, activation='softmax'))
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
In [25]:
print('started')
model.fit(X_tr.reshape(-1,3,224,224), Y_Keras, nb_epoch=500, batch_size=10,verbose=1
,validation_data=(X_ts.reshape(-1,3,224,224),Y_Keras_test ))
Y_kr= model.predict_classes(X_ts.reshape(-1,3,224,224))
print 'Accuracy of the model is ',accuracy_score(Y_ts,map[Y_kr],'\n')
#confusion_matrix(Y_ts,Y_kr_vales)
plot_confusion_matrix(map[Y_kr],Y_ts, classes=map,normalize=False,title='Confusion matrix')
In [33]:
clf = LogisticRegression(verbose=0,n_jobs=-1,multi_class='multinomial',solver='lbfgs',max_iter=50,warm_start=True)
clf.fit(X_normal,Y_number)
Y_logictic= clf.predict(X_ts.reshape(-1,9216))
#confusion_matrix(map[Y_logictic],Y_ts,labels=map)
print 'Accuracy of the model is ',accuracy_score(map[Y_logictic.astype(int)],Y_ts)
plot_confusion_matrix(map[Y_logictic.astype(int)],Y_ts, classes=map,normalize=True,title='Confusion matrix')
In [13]:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten,AveragePooling2D
from keras.layers import Convolution2D, MaxPooling2D,BatchNormalization
from keras.utils import np_utils
from keras.optimizers import Adam,SGD,Adadelta,Adagrad
from keras import backend as K
Y_Keras = np_utils.to_categorical(Y_number, 2691)
Y_Keras_test = np_utils.to_categorical(Y_test_number,2691)
model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96)))
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(64, 3, 3,border_mode='same'))
convout2 = Activation('relu')
model.add(convout2)
model.add(BatchNormalization(epsilon=1e-05,axis=1,momentum=0.99))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.5))
model.add(Convolution2D(64, 3, 3,border_mode='same'))
convout3 = Activation('relu')
model.add(convout3)
model.add(Convolution2D(128, 3, 3,border_mode='same'))
convout4 = Activation('relu')
model.add(convout4)
model.add(BatchNormalization(epsilon=1e-05,axis=1,momentum=0.99))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.5))
model.add(Convolution2D(96, 3, 3,border_mode='same'))
convout5 = Activation('relu')
model.add(convout5)
model.add(Convolution2D(192, 3, 3,border_mode='same'))
convout6 = Activation('relu')
model.add(convout6)
model.add(BatchNormalization(epsilon=1e-05,axis=1,momentum=0.99))
model.add(MaxPooling2D((2,2), strides=(2,2)))
#model.add(Convolution2D(128, 3, 3,border_mode='same'))
#convout7 = Activation('relu')
#model.add(convout7)
#model.add(Convolution2D(256, 3, 3,border_mode='same'))
#convout8 = Activation('relu')
#model.add(convout8)
#model.add(MaxPooling2D((2,2), strides=(2,2)))
#model.add(Convolution2D(160, 3, 3,border_mode='same'))
#convout9 = Activation('relu')
#model.add(convout9)
#model.add(Convolution2D(320, 3, 3,border_mode='same'))
#convout10 = Activation('relu')
#model.add(convout10)
#model.add(AveragePooling2D(pool_size=(2, 2), strides=(1,1)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(2691,activation='relu'))
model.add(Dense(2691,activation='softmax'))
adam = Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
In [14]:
%%time
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=40, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.3, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.3, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
datagen.fit(X_tr.reshape(-1,1,96,96))
model.fit_generator(datagen.flow(X_tr.reshape(-1,1,96,96), Y_Keras,
batch_size=40), nb_epoch=500,verbose=1,
samples_per_epoch = 20000,validation_data=(X_ts.reshape(-1,1,96,96),Y_Keras_test ))
In [15]:
model.save_weights("bigmodel_1.h5")
In [ ]:
import theano
convout1_f = theano.function([model.input], convout1.output)
convout2_f = theano.function([model.input], convout2.output)
convout3_f = theano.function([model.input], convout3.output)
convout4_f = theano.function([model.input], convout4.output)
convout5_f = theano.function([model.input], convout5.output)
convout6_f = theano.function([model.input], convout6.output)
#convout7_f = theano.function([model.input], convout7.output)
#convout8_f = theano.function([model.input], convout8.output)
#convout9_f = theano.function([model.input], convout9.output)
#convout10_f = theano.function([model.input], convout10.output)
# utility functions
from mpl_toolkits.axes_grid1 import make_axes_locatable
def nice_imshow(ax, data, vmin=None, vmax=None, cmap=None):
"""Wrapper around pl.imshow"""
if cmap is None:
cmap = cm.jet
if vmin is None:
vmin = data.min()
if vmax is None:
vmax = data.max()
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation='nearest', cmap=cmap)
plt.colorbar(im, cax=cax)
In [ ]:
import numpy.ma as ma
def make_mosaic(imgs, nrows, ncols, border=1):
"""
Given a set of images with all the same shape, makes a
mosaic with nrows and ncols
"""
nimgs = imgs.shape[0]
imshape = imgs.shape[1:]
mosaic = ma.masked_all((nrows * imshape[0] + (nrows - 1) * border,
ncols * imshape[1] + (ncols - 1) * border),
dtype=np.float32)
paddedh = imshape[0] + border
paddedw = imshape[1] + border
for i in xrange(nimgs):
row = int(np.floor(i / ncols))
col = i % ncols
mosaic[row * paddedh:row * paddedh + imshape[0],
col * paddedw:col * paddedw + imshape[1]] = imgs[i]
return mosaic
#pl.imshow(make_mosaic(np.random.random((9, 10, 10)), 3, 3, border=1))
In [14]:
import matplotlib.pyplot as plt
%matplotlib inline
i = 12
# Visualize the first layer of convolutions on an input image
sample = X_ts[i:i+1]
plt.figure()
plt.title('input')
nice_imshow(plt.gca(), np.squeeze(sample), vmin=0, vmax=1, cmap=cm.Greys_r)
In [27]:
# Visualize convolution result (after activation)
C1 = convout1_f(sample.astype(np.float32).reshape(-1,1,96,96))
C1 = np.squeeze(C1)
print("C1 shape : ", C1.shape)
plt.figure(figsize=(15, 15))
plt.suptitle('convout1')
nice_imshow(plt.gca(), make_mosaic(C1, 6, 6), cmap=cm.Greys_r)
In [15]:
from keras.models import model_from_json,model_from_yaml
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
json_file.write(model_json)
print("Saved model to disk")
In [15]:
%%time
from keras.models import model_from_json,model_from_yaml
json_file = open('/home/mckc/Face_code/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("/home/mckc/Face_code/model.h5")
print("Loaded model from disk")
In [ ]:
from keras.models import model_from_json,model_from_yaml
json_file = open('/home/pi/Desktop/files/model_backup.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
In [17]:
# serialize model to YAML
model_yaml = loaded_model.to_yaml()
with open("model.yaml", "w") as yaml_file:
yaml_file.write(model_yaml)
In [18]:
# load YAML and create model
yaml_file = open('/home/pi/Desktop/files/model.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)
# load weights into new model
loaded_model.load_weights("/home/mckc/Face_code/model.h5")
print("Loaded model from disk")
In [ ]: